package com.zhiyizhen.handheld.component.pharmacy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.pharmacy.bean.GroupListBean;
import com.zhiyizhen.handheld.component.pharmacy.view.GiveShuyeUnitLayout;
import com.zhiyizhen.handheld.databinding.ItemGiveShuyeGroupBinding;

public class GiveShuyeGroupAdapter extends BaseAdapter<GroupListBean, GiveShuyeGroupAdapter.ShuyeGroupViewHolder> {

    public GiveShuyeGroupAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ShuyeGroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ShuyeGroupViewHolder(this, ItemGiveShuyeGroupBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class ShuyeGroupViewHolder extends BaseViewHolder<GroupListBean> {

        private final ItemGiveShuyeGroupBinding binding;

        public ShuyeGroupViewHolder(BaseAdapter<GroupListBean, ShuyeGroupViewHolder> adapter, ItemGiveShuyeGroupBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(GroupListBean data) {
            binding.groupNameTv.setText(data.getGroupName());
            binding.usageCodeTextTv.setText(data.getUsageCodeText());
            binding.frequencyCodeTextTv.setText(data.getFrequencyCodeText());
            binding.totalDayTv.setText(data.getCycle() + data.getCycleUnitText());

            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            if (data.getDetailList() != null && !data.getDetailList().isEmpty()) {
                GiveShuyeUnitLayout shuyeUnitLayout;
                for (int i = 0; i < data.getDetailList().size(); i++) {
                    shuyeUnitLayout = new GiveShuyeUnitLayout(itemView.getContext(), i + 1, data.getDetailList().get(i));
                    binding.shuyeInfoLayout.addView(shuyeUnitLayout);
                }
            }
        }
    }

}